package com.hrs.common.exception;

import com.hrs.common.result.CodeMsg;
import com.hrs.common.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.stereotype.Controller;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolationException;
import java.net.BindException;
import java.util.List;

@ControllerAdvice(annotations = {RestController.class, Controller.class})//捕捉范围
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(HttpServletRequest request, Exception e) {
        //自定义异常
        if(e instanceof GlobalException){
            return Result.error(((GlobalException) e).getCodeMsg());
        }

        // http 请求方式错误
        if(e instanceof HttpRequestMethodNotSupportedException){
            return Result.error(e.getMessage());
        }

        // 参数类型错误
        if(e instanceof MethodArgumentTypeMismatchException){//参数类型错误异常捕捉
            String message = e.getMessage();
            String[] split = message.split(";");
            return Result.error(split[0]);
        }

        //json格式错误异常捕捉
        if (e instanceof HttpMessageNotReadableException){
            String message = e.getMessage();
            String[] split = message.split(";");
            return Result.error(split[0]);
        }

        // 参数校验未通过
        if (e instanceof MethodArgumentNotValidException) {
            String message = e.getMessage();
            String error = message.substring(message.lastIndexOf("[") + 1, message.lastIndexOf("]") - 1);
            return Result.error(error);
        }

        if (e instanceof DataIntegrityViolationException) {
            String[] split = e.getMessage().split(":");
            return Result.error(split[split.length - 1]);
        }

        if(e instanceof IllegalArgumentException) {
            return Result.error("目标不存在");
        }

        if(e instanceof MaxUploadSizeExceededException){
            return Result.error("图片文件不得大于3MB");
        }

        if (e instanceof ConstraintViolationException) {
            return Result.error(e.getMessage());
        }

        e.printStackTrace();
        log.error(e.toString());

        return Result.error(CodeMsg.SERVER_INTERNAL_ERROR);
    }
}